/*
 * @Author: 王雁鹏
 * @Date: 2023-06-14 22:26:48
 * @LastEditTime: 2023-07-31 19:00:21
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: \expressf:\web-vue\bs-nodejs-background-code\routes\order\service.js
 * -------------------------------------
 */
const request = require('request');
var req = require("../../utils/router");
const db = require("../../mysql/index.js");
const config = require("./config.js");
const { generateUUID, randomNumber, createTime } = require("../../utils/func.js");
const { Params } = require('../../mysql/index.js');
let { router, GetRouter, PostRouter } = req

class Order {
    constructor() {

    }
    getOrder(res, data = {}) {
        let { openid } = data
        let obj = {
            user_id: db.EQ({ ...config, select: { user_id: openid } }),
            del: db.EQ({ ...config, select: { del: "1" } }),
            status: db.EQ({ ...config, select: { status: "3" } }),
        }
        let select = db.Params(obj)
        db.select({
            db: 'vw_shop',
            select,
        }, (rows) => {
            res.send({ code: 200, data: rows });
        })
    }
    setOrder() {

    }
}
let getOrder = (rows) => {
    return rows.reduce((current, item) => {
        let numBool = 0
        let arr = current.map(params => {
            let arr = params.list
            let codeBool = arr.filter(i => i.code == item.code).length > 0
            let prodBool = arr.filter(i => i.produce_id == item.produce_id).length > 0
            if (codeBool) {
                numBool++
            }
            if (codeBool && prodBool) {
                params.list = arr.map(i => {
                    if (i.code === item.code && i.produce_id == item.produce_id) {
                        return { ...i, num: Number(i.num) + 1 }
                    }
                    return i
                })
            } else if (codeBool && !prodBool) {
                params.list.push(item)
            }
            params.price = params.list.reduce((price, item) => {
                price = price + (item.produce_price * item.num)
                return price
            }, 0)
            params.total = params.list.reduce((num, item) => {
                num = Number(item.num) + num
                return num
            }, 0)
            return params
        })
        if (numBool == 0 || arr.length == 0) {
            arr.push({
                price: item.produce_price * item.num,
                code: item.code,
                time: item.time,
                type: item.type,
                status: item.status,
                total: 1,
                list: [item]
            })
        }
        if (arr.length > 0) {
            arr.forEach(i => {
                i.time = createTime({ time: i.time })
                i.timeDelay = createTime({ time: i.time, minutes: 30 })
                if (i.list.length > 0) {
                    i.list.forEach(f => {
                        f.time = createTime({ time: f.time })
                        f.timeDelay = createTime({ time: i.time, minutes: 30 })
                    })
                }
            })
        }
        return arr
    }, [])
}
// 添加到购物车
new PostRouter().send('/shop', function (res, data) {
    let { openid, produce_id, num } = data
    const time = createTime()
    if (openid) {
        let obj = {
            code: db.EQ({ ...config, select: { code: openid } }),
            del: db.EQ({ ...config, select: { del: "1" } }),
            // status: db.EQ({ ...config, select: { status: "3" } }),
        }
        let select = db.Params(obj)
        db.select({
            db: 'wh_shop',
            select
        }, (data) => {
            if (data.list.length > 0) {
                let { status, id } = data.list[0]
                if (status !== "3") {
                    let condition = { status: "3" }
                    let params = {
                        db: "wh_shop",
                        id,
                        condition
                    }
                    db.setData(params)
                }
                let condition = { user_id: openid, produce_id, num, code: openid, time }
                db.setData({
                    db: 'wh_shop_list',
                    condition
                }, (rows, fields) => {
                    res.send({ code: 200, data: rows });
                })
            } else {
                let condition = { user_id: openid, num, code: openid, time, status: "3" }
                db.setData({
                    db: 'wh_shop',
                    condition
                }, (rows) => {
                    let condition = { user_id: openid, status: "3", produce_id, num, code: openid, time }
                    db.setData({
                        db: 'wh_shop_list',
                        condition
                    }, (rows, fields) => {
                        res.send({ code: 200, data: rows });
                    })
                })
            }

        })


    } else {
        res.send({ code: "100", msg: "缺少用户id" })
    }
})
// 获取全部订单,1是未支付订单，2，是已支付订单
new GetRouter().send('/getShop', (res, data) => {
    let { openid } = data
    let obj = {
        user_id: db.EQ({ db: "vw_shop", select: { user_id: openid } }),
        del: db.EQ({ db: "vw_shop", select: { del: "1" } }),
        status: db.EQ({ db: "vw_shop", select: { status: "3" } }),
    }
    let select = db.Params(obj)
    db.select({
        db: 'vw_shop',
        select,
    }, (rows) => {
        if (rows.list.length > 0) {
            let data = getOrder(rows.list)
            res.send({
                code: 200, data: data[0].list.map(item => {
                    return { ...item, id: item.produce_id }
                }),
            });
        } else {
            res.send({ code: 200, data: [], });
        }
    })
})

// 获取全部订单,1是未支付订单，2，是已支付订单
new GetRouter().send('/changeShop', (res, data) => {
    let { openid, produce_id, num } = data
    let obj = {
        code: db.EQ({ db: "wh_shop_list", select: { code: openid } }),
        produce_id: db.EQ({ db: "wh_shop_list", select: { produce_id } }),
    }
    let select = db.Params(obj)
    db.select({
        db: 'wh_shop_list',
        select,
    }, (rows) => {
        if (rows.list.length > 0) {
            let condition = { ...rows.list[0] }
            if (rows.list.length > num) {
                let { id } = rows.list[0]
                let obj = {
                    id: db.EQ({ ...config, select: { id } }),
                }
                let select = db.Params(obj)
                db.delete({
                    ...config,
                    select,
                }, (rows) => {
                    res.send({ code: 200, data: rows });
                })
            } else if (rows.list.length < num) {
                delete condition.id
                db.setData({
                    ...config,
                    condition
                }, (rows, fields) => {
                    res.send({ code: 200, data: rows });
                })
            }
        } else {
            res.send({ code: 200, data: [], });
        }
    })
})
// 删除菜单
new PostRouter().send('/delShop', (res, data) => {
    let { id } = data
    let obj = { id: db.EQ({ ...config, select: { id } }) }
    let select = db.Params(obj)
    db.delete({
        ...config,
        select,
    }, (rows) => {
        res.send({ code: 200, data: rows });
    })
})


// 获取全部订单,1是未支付订单，2，是已支付订单
new GetRouter().send('/getOrderForm', (res, data) => {
    let { openid } = data
    if (!openid) {
        return res.send({ code: "100", data: [], msg: "未登录" })
    }
    let obj = {
        user_id: db.EQ({ db: "vw_order", select: { user_id: openid } }),
        // del: db.EQ({ ...config, select: { del: "1" } }),
        // status: db.IN({ db: "vw_order", select: { status: ["1", "2"] } }),
    }
    let select = db.Params(obj)
    db.select({
        db: 'vw_order',
        select
    }, (rows) => {
        if (rows.list.length > 0) {
            let list = getOrder(rows.list)
            db.select({
                db: 'vw_order_score',
                select
            }, (rows) => {
                let data = getOrder(rows.list)
                res.send({ code: 200, data: [...list, ...data] });
            })
        } else {
            res.send({ code: 200, data: [] });
        }
    })
})

new GetRouter().send('/getAllOrderForm', (res, data) => {
    let { pageSize, pageNum, status, code } = data
    let obj = {
        code: db.EQ({ db: "vw_order", select: { code } }),
        status: db.EQ({ db: "vw_order", select: { status } }),
    }
    let select = db.Params(obj)
    db.select({
        db: 'vw_order',
        select,
        pageSize, pageNum
    }, (rows) => {
        if (rows.list.length > 0) {
            let list = getOrder(rows.list)
            db.select({
                db: 'vw_order_score',
                select
            }, (rows) => {
                let data = getOrder(rows.list)
                res.send({ code: 200, data: [...list, ...data] });
            })
        } else {
            res.send({ code: 200, data: [] });
        }
    })
})
module.exports = router

